using System;
using System.Collections.Generic;
using System.Text;
using FireStarter.CodeGeneration.Generator;
using FireStarterModeller.BO;

namespace FireStarter.CodeGeneration
{
    //public class DMClass : IModelClass
    //{
    //    private readonly List<IModelProperty> _properties = new List<IModelProperty>();
    //    private readonly List<IModelRelationship> _relationships = new List<IModelRelationship>();

    //    public DMClass()
    //    {
    //        PrimaryKeyProperties = new List<IModelProperty>();
    //        AlternateKeys = new List<IModelKey>();
    //    }

    //    public string ClassName { get; set; }
    //    public string DisplayName { get; set; }
    //    public string AssemblyName { get; set; }
        
    //    public IModelClass SuperClass
    //    {
    //        get
    //        {
    //            if (string.IsNullOrEmpty(SuperClassName)) return null;
    //            if (Parent == null) throw new GenerationException(
    //                "DMClass '" + ClassName + "' does not have its parent set");

    //            IModelSolution modelProject = (IModelSolution) Parent;
    //            foreach (IModelClass dmClass in modelProject.ModelClasses)
    //            {
    //                if (dmClass.ClassName == SuperClassName &&
    //                    dmClass.AssemblyName == SuperClassAssemblyName)
    //                {
    //                    return dmClass;
    //                }
    //            }
                
    //            throw new GenerationException(string.Format(
    //                "While attempting to load a SuperClass for '{0}', no class could be found for the given " +
    //                "SuperClassName and SuperClassAssemblyName.", ClassName));
    //        }
    //    }

    //    public string SuperClassName { get; set; }
    //    public string SuperClassAssemblyName { get; set; }
    //    public string SuperClassChildID { get; set; }
    //    public string SuperClassDiscriminator { get; set; }
    //    public string InheritanceType { get; set; }
    //    public string TableName { get; set; }

    //    /// <summary>
    //    /// Gets the properties in this class only, excluding inherited ones
    //    /// </summary>
    //    public IList<IModelProperty> Properties
    //    {
    //        get { return _properties; }
    //    }

    //    /// <summary>
    //    /// Gets all the properties in this class and inherited from parents
    //    /// </summary>
    //    public IList<IModelProperty> AllProperties
    //    {
    //        get
    //        {
    //            List<IModelProperty> allProperties = GetModelPropertyCollectionClone(_properties);

    //            IModelClass superClass = SuperClass;
    //            if (superClass != null)
    //            {
    //                foreach (IModelProperty modelProperty in superClass.AllProperties)
    //                {
    //                    IModelProperty clone = GetModelPropertyClone(modelProperty);
    //                    clone.Parent = this;
    //                    allProperties.Add(clone);
    //                }
    //            }
    //            return allProperties;
    //        }
    //    }

    //    private static List<IModelProperty> GetModelPropertyCollectionClone(List<IModelProperty> modelProperties)
    //    {
    //        List<IModelProperty> clonedCollection = new List<IModelProperty>();
    //        foreach (IModelProperty modelProperty in modelProperties)
    //        {
    //            clonedCollection.Add(GetModelPropertyClone(modelProperty));
    //        }
    //        return clonedCollection;
    //    }

    //    private static IModelProperty GetModelPropertyClone(IModelProperty modelProperty)
    //    {
    //        IModelProperty clone = new DMProperty();
    //        clone.DefaultValue = modelProperty.DefaultValue;
    //        clone.DisplayName = modelProperty.DisplayName;
    //        clone.IsCompulsory = modelProperty.IsCompulsory;
    //        clone.LookupListType = modelProperty.LookupListType;
    //        clone.Max = modelProperty.Max;
    //        clone.Min = modelProperty.Min;
    //        clone.Parent = modelProperty.Parent;
    //        clone.PropertyName = modelProperty.PropertyName;
    //        clone.PropertyType = modelProperty.PropertyType;
    //        clone.ReadWriteRule = modelProperty.ReadWriteRule;
    //        return clone;
    //    }

    //    /// <summary>
    //    /// Gets the relationships owned by this class only, excluding inherited ones
    //    /// </summary>
    //    public IList<IModelRelationship> Relationships
    //    {
    //        get { return _relationships; }
    //    }

    //    /// <summary>
    //    /// Gets the relationships owned by this class and inherited from parents
    //    /// </summary>
    //    public IList<IModelRelationship> AllRelationships
    //    {
    //        get
    //        {
    //            List<IModelRelationship> allProperties = GetModelRelationshipCollectionClone(_relationships);

    //            IModelClass superClass = SuperClass;
    //            if (superClass != null)
    //            {
    //                foreach (IModelRelationship modelRelationship in superClass.AllRelationships)
    //                {
    //                    IModelRelationship clone = GetModelRelationshipClone(modelRelationship);
    //                    clone.Parent = this;
    //                    allProperties.Add(clone);
    //                }
    //            }
    //            return allProperties;
    //        }
    //    }

    //    private static List<IModelRelationship> GetModelRelationshipCollectionClone(List<IModelRelationship> modelRelationships)
    //    {
    //        List<IModelRelationship> clonedCollection = new List<IModelRelationship>();
    //        foreach (IModelRelationship modelRelationship in modelRelationships)
    //        {
    //            clonedCollection.Add(GetModelRelationshipClone(modelRelationship));
    //        }
    //        return clonedCollection;
    //    }

    //    private static IModelRelationship GetModelRelationshipClone(IModelRelationship modelRelationship)
    //    {
    //        IModelRelationship clone = new DMRelationship();
    //        clone.Parent = modelRelationship.Parent;
    //        clone.DeleteParentAction = modelRelationship.DeleteParentAction;
    //        clone.IsMultiple = modelRelationship.IsMultiple;
    //        clone.Property = modelRelationship.Property;
    //        clone.PropertyName = modelRelationship.PropertyName;
    //        clone.RelatedAssembly = modelRelationship.RelatedAssembly;
    //        clone.RelatedClass = modelRelationship.RelatedClass;
    //        clone.RelatedProperty = modelRelationship.RelatedProperty;
    //        clone.RelatedPropertyName = modelRelationship.RelatedPropertyName;
    //        clone.RelationshipName = modelRelationship.RelationshipName;
    //        return clone;
    //    }

    //    public IModelElement Parent { get; set; }
    //    public IList<IModelProperty> PrimaryKeyProperties { get; internal set; }
    //    public bool PrimaryKeyIsID { get; set; }
    //    public IList<IModelKey> AlternateKeys { get; internal set; }

    //    public bool HasSuperClass
    //    {
    //        get { return !string.IsNullOrEmpty(SuperClassName); }
    //    }

    //    public bool SuperClassDefIDIsEmpty
    //    {
    //        get { return SuperClassChildID==""; }
    //    }

    //    public bool SuperClassDefIDIsNotNullOrEmpty
    //    {
    //        get { return !string.IsNullOrEmpty(SuperClassChildID); }
    //    }

    //    public bool SuperClassDefIDIsNull
    //    {
    //        get { return SuperClassChildID == null; }
    //    }
    //}

    //public class IModelProperty : IModelProperty
    //{
    //    private string _defaultValue;

    //    public IModelProperty()
    //    {
    //        PropertyType = new IModelPropertyType();
    //    }

    //    public string PropertyName { get; set; }
    //    public IModelPropertyType PropertyType { get; set; }

    //    public string PropertyTypeString
    //    {
    //        get { return PropertyType.AssemblyName + "." + PropertyType.TypeName; }
    //    }

    //    public bool HasSetter { get; set; }

    //    public string DefaultValue
    //    {
    //        get
    //        {
    //            if (PropertyType != null && PropertyType.TypeName != null)
    //            {
    //                if (_defaultValue != null && PropertyType.TypeName.ToUpper() == "STRING")
    //                {
    //                    return String.Format(@"""{0}""", _defaultValue);
    //                }
    //            }
    //            return _defaultValue;
    //        }
    //        set { _defaultValue = value; }
    //    }

    //    public bool IsCompulsory { get; set; }

    //    public string ValidValue
    //    {
    //        get
    //        {
    //            return ValidValueGenerator.GetValidValue(this);
    //        }
    //    }

    //    public IModelElement Parent { get; set; }
    //    public string Max { get; set; }
    //    public string Min { get; set; }
    //    public ModelLookupType LookupListType { get; set; }
    //    public string DisplayName { get; set; }
    //    public string ReadWriteRule { get; set; }
    //}

    //public class IModelPropertyType : IModelPropertyType
    //{
    //    public string TypeName { get; set; }
    //    public string AssemblyName { get; set; }
    //    public bool IsValueType { get; set; }
    //    public IModelElement Parent { get; set; }
    //}

    //public class ModelKey : IModelKey
    //{
    //    public ModelKey()
    //    {
    //        KeyProperties = new List<IModelProperty>();
    //    }

    //    public IModelElement Parent { get; set; }
    //    public string KeyName { get; set; }
    //    public IList<IModelProperty> KeyProperties { get; internal set; }
    //}

    //public class IModelRelationship : IModelRelationship
    //{
    //    public string RelationshipName { get; set; }
    //    public string RelatedClass { get; set; }
    //    public string RelatedAssembly  { get; set; }
    //    public bool IsMultiple { get; set; }
    //    public IModelElement Parent { get; set; }
    //    public IModelProperty Property { get; set; }
    //    public IModelProperty RelatedProperty { get; set; }

    //    public string RelatedPropertyName
    //    {
    //        get { return RelatedProperty.PropertyName; }
    //        set {  }
    //    }

    //    public string PropertyName
    //    {
    //        get { return Property.PropertyName; }
    //        set {  }
    //    }

    //    public string DeleteParentAction { get; set; }
    //}

    //public class IModelSolution : IModelSolution
    //{
    //    public IModelSolution()
    //    {
    //        ModelClasses = new List<IModelClass>();
    //    }

    //    public IModelElement Parent { get; set; }
    //    public IList<IModelClass> ModelClasses { get; set; }
    //    public string BOProjectName { get; set; }
    //    public string ProjectName { get; set; }
    //}
}
